import fetch from '../config/fetch'
import {cookie} from '../config/utils' // trim

export const getI9RIA = () => {
  var p = new Promise(function (resolve, reject) {
    // 做一些异步操作
    let num = 0
    let getTmpData = () => (setTimeout(function () {
      if (window.I9RIA && window.I9RIA.unionid && window.I9RIA.wxJsApiConf) {
        resolve(window.I9RIA)
      } else {
        num++
        if (num > 30) {
          resolve({code: 9999})
        } else {
          getTmpData()
        }
      }
    }, 300))
    if (window.I9RIA && window.I9RIA.unionid && window.I9RIA.wxJsApiConf) {
      resolve(window.I9RIA)
    } else {
      getTmpData()
    }
  })
  return p
}
export const clickRecord = (tid, other) => {
  let param = other ? {'c': tid, 'a': other} : {tid}
  fetch('/Api/Click/Record', { // 导航点击记录
    type: 'POST', data: param, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
  })
}

export const duringRecord = (c, a, t) => fetch('/Api/Click/DuringRecord', { // 记录时长
  type: 'POST', data: {c, a, t}, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const areaRecord = (city) => fetch('/Api/Click/AreaRecord', { // 地理位置
  type: 'POST', data: {city}, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const creditRecordChkSubscribe = () => fetch('/Api/CreditRecord/ChkUpdated', { // 积分记录
  type: 'POST', headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const ChkSubscribe = () => fetch('/Api/User/ChkSubscribe', { // 获取游戏列表
  type: 'POST', headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const latestRecord = () => fetch('/Api/point/LatestRecord', { // 获取游戏列表
  type: 'POST', headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const getBanner = (type) => fetch('/Api/Banner', { // 获取Banner
  type: 'POST', data: {type}, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const bannerRecord = (aid) => fetch('/Api/Banner/Record', { // 统计点击Banner
  type: 'POST', data: {aid}, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const getGame = (p, areaId, sortType) => fetch('/Api/Game', { // 获取游戏列表(福利)
  type: 'POST', data: {p, 'area_id': areaId || 0, 'sort_type': sortType}, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const getGamePromotion = (p) => fetch('/Api/game/promotion', { // 获取游戏列表（积分）
  type: 'POST', data: {p}, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const getPointTimes = (appid) => fetch('/Api/point/GetPointTimes', { // 获取详情页信息（福利）
  type: 'POST', data: {appid}, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const subsribeCntToday = () => fetch('/Api/point/SubsribeCntToday', { // 获取任务完成情况
  type: 'POST', headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const getShop = (p) => fetch('/Api/Shop', { // 获取游戏列表
  type: 'POST', data: {unionid: cookie('unionid'), p}, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const shopExchange = (pid) => fetch('/Api/Shop/Exchange', { // 积分兑换
  type: 'POST', data: {pid}, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const shopChkMobile = (json) => fetch('/Api/Shop/chkMobile', { // 积分兑换
  type: 'POST', data: json, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const getUserCredit = () => fetch('/Api/User/Credit', { // 获取用户平台积分余额接口
  type: 'POST', headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const getUserCreditRecord = (p, listRow) => fetch('/Api/User/CreditRecord', { // 获取用户积分记录接口
  type: 'POST', data: {p, 'list_row': listRow}, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const getUserGameRecord = (p, listRow, search) => fetch('/Api/User/GameRecord', { // 用户玩过的游戏列表接口
  type: 'POST', data: {p, 'list_row': listRow, search}, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const getUserGameDetail = (appid) => fetch('/Api/User/GameDetail', { // 用户玩过的游戏详情
  type: 'POST', data: {appid}, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const gameRecord = (appid, cate) => fetch('/Api/Game/Record', { // 统计
  type: 'POST', data: {appid, cate}, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const shareRecord2 = (appid, sharelink) => fetch('/Api/Record/Wxshare', { // 分享统计
  type: 'GET', data: {appid, unionid: cookie('unionid'), sharelink}, headers: {}
}, true)

export const shareRecord = (appid, sharelink) => fetch('/Api/aw/wzshare', { // 分享统计
  type: 'POST', data: {appid, unionid: cookie('unionid'), sharelink}, headers: {}
})

export const getUserExchangeRecord = (p, listRow) => fetch('/Api/User/ExchangeRecord', { // 我的兑换记录列表接口
  type: 'POST', data: {p, 'list_row': listRow}, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const getUserPromotion = () => fetch('/Api/User/Promotion', { // 我的兑换记录列表接口
  type: 'POST', headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const userFaq = (content) => fetch('/Api/User/Faq', { // 我的兑换记录列表接口
  type: 'POST', data: {content}, headers: {unionid: cookie('unionid')}
})

export const userPersonality = () => fetch('/Api/User/Personality', { // 显示用户兴趣定制接口
  type: 'POST', headers: {unionid: cookie('unionid')}
})

export const userSetPersonality = (birthday, sex, interest) => fetch('/Api/User/SetPersonality', { // 设置用户兴趣定制接口
  type: 'POST', data: {birthday, sex, interest}, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const signed = () => fetch('/Api/User/Signed', { // 签到
  type: 'POST', headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const getGameTypeList = (pid) => fetch('/Api/ExternalGame/Category', { // 获取玩游戏类型列表
  type: 'POST', data: {pid}, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const getGameList = (p, cid, listRow) => fetch('/Api/ExternalGame', { // 获取玩游戏列表
  type: 'POST', data: {p, cid, 'list_row': listRow || 6}, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const setGameRecord = (tid) => fetch('/Api/ExternalGame/Record', { // 统计点击玩游戏
  type: 'POST', data: {tid}, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const getCouponTypeList = (pid) => fetch('/Api/ExternalCoupon/Category', { // 获取优惠券类型列表
  type: 'POST', data: {pid}, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const getCouponList = (p, search, cid, listRow, recommend) => fetch('/Api/ExternalCoupon', { // 获取优惠券数据列表
  type: 'POST', data: {p, cid, 'list_row': listRow || 6, recommend, search}, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const setCouponRecord = (tid) => fetch('/Api/ExternalCoupon/Record', { // 统计点击优惠券
  type: 'POST', data: {tid}, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const getFanliList = (p, search, order, listRow) => fetch('/Api/fanli/List', { // 返佣金列表数据
  type: 'POST', data: {'pagesize': listRow || 6, 'pageno': p, order, search}, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const getFanliItem = (id) => fetch('/Api/fanli/Item', { // 返佣金商品详情
  type: 'POST', data: {id}, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const getFanliOrderList = (p, status, listRow) => fetch('/Api/fanli/OrderList', { // 返佣金记录列表数据
  type: 'POST', data: {'pagesize': listRow || 6, 'pageno': p, status}, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const getSaleList = (p, search, listRow) => fetch('/Api/Sale', { // 9.9包邮商品列表
  type: 'POST', data: {'list_row': listRow || 8, p, search}, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const addCart = (goodsId) => fetch('/Api/Sale/AddCart', { // 9.9包邮加购物车
  type: 'POST', data: {'goods_id': goodsId}, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const getCart = () => fetch('/Api/Sale/GetCart', { // 9.9包邮购物车信息
  type: 'POST', headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const getAddress = () => fetch('/Api/Sale/Address', { // 9.9包邮获取收获地址
  type: 'POST', headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const updateAddress = (addressId, consignee, province, provinceName, city, cityName, district, districtName, address, mobile) => fetch('/Api/Sale/UpdateAddress', { // 9.9包邮维护收获地址
  type: 'POST', data: {'address_id': addressId, consignee, province, 'province_name': provinceName, city, 'city_name': cityName, district, 'district_name': districtName, address, mobile}, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const wxPayOrder = (consignee, orderProduct) => fetch('/Api/WxPay/AddOrder', { // 微信电商支付
  type: 'POST', data: {'param': '&' + paramToString({'consignee': consignee, 'order_product': orderProduct})}, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const getOrder = (orderSnNo, p, listRow) => fetch('/Api/WxPay/GetOrder', { // 微信电商支付
  type: 'POST', data: {'order_sn_no': orderSnNo, p, 'list_row': listRow}, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const getWZTInfo = (appid) => fetch('/Api/Aw/awExt', { // 玩赚推媒体详情
  type: 'POST', data: {appid}, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const isFirstWZT = (appid, openid) => fetch('/Api/Aw/isFirst', { // 首次打开玩赚推媒体
  type: 'POST', data: {appid, openid}, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const getWZTScore = (appid, openid) => fetch('/Api/Aw/calcScore', { // 玩赚推媒体获取积分详情
  type: 'POST', data: {appid, openid}, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const judgeReward = (appid, openid, url, code) => fetch('/Api/Aw/judgeReward', { // 玩赚推媒体浏览时长加分
  type: 'POST', data: {appid, openid, url, code}, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const getRedPackList = (search, p, listRow, cid) => fetch('/Api/ExternalRedPack', { // 抢红包
  type: 'POST', data: {search, p, 'list_row': listRow, cid}, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const getRedPackInfo = (tid) => fetch('/Api/ExternalRedPack/Item', { // 抢红包详情
  type: 'POST', data: {tid}, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

export const redPackRecord = (tid, from) => fetch('/Api/ExternalRedPack/Record', { // 抢红包详情点击统计
  type: 'POST', data: {tid, from}, headers: {unionid: cookie('unionid'), redirect: window.location.hash}
})

// 处理多层数据
let paramToString = (param, key) => {
  let paramStr = ''
  if (typeof param === 'string' || typeof param === 'number' || typeof param === 'boolean') {
    paramStr += '&' + key + '=' + encodeURIComponent(param)
  } else if (Array.prototype.isPrototypeOf(param) && param.length === 0) {
    paramStr += '&' + key + '=[]'
  } else if (param == null) {
    paramStr += '&' + key + '=null'
  } else {
    for (let i in param) {
      let k = key == null ? i : key + '[' + i + ']'
      paramStr += '&' + paramToString(param[i], k)
    }
    if (paramStr === '') {
      paramStr += '&' + key + '={}'
    }
  }
  return paramStr.substr(1)
}
